<template>
  <p>不可重复执行！</p>
  <el-button
    type="primary"
    :loading="loadData.table"
    @click="submitRepairField()"
    :disabled="props.isDisabled"
  >
    根据“订单”更新收益表
  </el-button>
  <el-button
    type="primary"
    :loading="loadData.table"
    @click="submitRepairByCapital()"
    :disabled="props.isDisabled"
  >
    根据“提现记录”更新收益表
  </el-button>
  <el-button
    type="primary"
    :loading="loadData.table"
    @click="submitRepairByThaw()"
    :disabled="props.isDisabled"
  >
    根据“订单”解冻会员收益
  </el-button>
  <el-button
    type="primary"
    :loading="loadData.table"
    @click="submitRepairByUserIncome()"
    :disabled="props.isDisabled"
  >
    调整部分用户的收入
  </el-button>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { getRepairList, actionRepairModule } from '@/apis/repair';

// props
const props = defineProps({
  isDisabled: {
    type: Boolean,
    default: false
  }
});

const loadData = reactive({
  table: false
});

/**
 * 更新字段
 */
const submitRepairField = async (page: number = 1) => {
  loadData.table = true;

  // 每次取20条数据
  const maxLimit = 1;
  const resList = await actionRepairModule('getOrderPageList', {
    page,
    limit: maxLimit
  });

  if (!resList.data.data || !resList.data.data.length) {
    console.warn('执行结束');
    loadData.table = false;
    return;
  }

  // 循环所有数据
  for (let i = 0; i < resList.data.data.length; i++) {
    const item = resList.data.data[i];

    console.group('更新table字段');
    console.log(
      `[Limit] - ${maxLimit} -- [Page] - ${page} -- [Current] - ${i + 1}`
    );
    console.log(`[ID] - ${item.id}`);
    console.groupEnd();

    try {
      await actionRepairModule('createIncomeByOrder', { id: item.id });
    } catch (error) {
      console.error('submitRepairField', error);
      loadData.table = false;
      return;
    }
  }

  setTimeout(() => {
    submitRepairField(page + 1);
  }, 1 * 1000);
};

const submitRepairByCapital = async (page: number = 1) => {
  loadData.table = true;

  // 每次取20条数据
  const maxLimit = 1;
  const resList = await getRepairList('user-capital', {
    page,
    limit: maxLimit
  });

  if (!resList.data.data || !resList.data.data.length) {
    console.warn('执行结束');
    loadData.table = false;
    return;
  }

  // 循环所有数据
  for (let i = 0; i < resList.data.data.length; i++) {
    const item = resList.data.data[i];

    console.group('更新table字段');
    console.log(
      `[Limit] - ${maxLimit} -- [Page] - ${page} -- [Current] - ${i + 1}`
    );
    console.log(`[ID] - ${item.id}`);
    console.groupEnd();

    try {
      await actionRepairModule('createIncomeByCapital', { id: item.id });
    } catch (error) {
      console.error('submitRepairByCapital', error);
      loadData.table = false;
      return;
    }
  }

  setTimeout(() => {
    submitRepairByCapital(page + 1);
  }, 1 * 1000);
};

// 解冻会员收益
const submitRepairByThaw = async (page: number = 1) => {
  loadData.table = true;

  // 每次取20条数据
  const maxLimit = 1;
  const resList = await actionRepairModule('getOrderPageList', {
    page,
    limit: maxLimit
  });

  if (!resList.data.data || !resList.data.data.length) {
    console.warn('执行结束');
    loadData.table = false;
    return;
  }

  // 循环所有数据
  for (let i = 0; i < resList.data.data.length; i++) {
    const item = resList.data.data[i];

    console.group('更新table字段');
    console.log(
      `[Limit] - ${maxLimit} -- [Page] - ${page} -- [Current] - ${i + 1}`
    );
    console.log(`[ID] - ${item.id}`);
    console.groupEnd();

    try {
      await actionRepairModule('thawIncomeByOrder', { id: item.id });
    } catch (error) {
      console.error('submitRepairByThaw', error);
      loadData.table = false;
      return;
    }
  }

  setTimeout(() => {
    submitRepairByThaw(page + 1);
  }, 1 * 1000);
};

/**
 * 调整部分用户的收入（积分、余额）
 */
const submitRepairByUserIncome = async () => {
  loadData.table = true;

  try {
    await actionRepairModule('updateUserCreditsBonus', null);
  } catch (error) {
    console.error('submitRepairByUserIncome', error);
    loadData.table = false;
    return;
  }

  console.warn('执行结束');
  loadData.table = false;
};
</script>

<style lang="scss" scoped></style>
